<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <!--公用头部-->
    <th:block th:replace="common/head::commonHead('角色新增-小邑')" />
    <style>
        #formAdvForm .layui-row {
            margin-top: 20px;
        }
        #formAdvForm .layui-form-item {
            margin-bottom: 0;
        }
        #formAdvForm .layui-form-item .layui-inline {
            margin-bottom: 25px;
            margin-right: 0;
        }
    </style>
</head>
<!--公用js-->
<th:block th:replace="common/head::commonJs"/>
<script>
    var roleId = null;
    var el = null;
    $(function () {
        roleId = utils.request.getQueryString("roleId");
    })
</script>
<body marginwidth="0" marginheight="0">
<form class="layui-form" action="" id="formAdvForm" lay-filter="formAdvForm">
    <div class="layui-fluid  layui-fluid-btn">
        <div class="layui-col-md12">
            <div class="layui-card"  style="margin: 10px;">
                <div class="layui-card-body">
                    <input name="id" type="text" style="display: none">
                    <div class="layui-form-item layui-row">
                        <div class="layui-inline layui-col-md12" id="authTree"></div>
                    </div>
                    <div class="form-group-bottom">
                        <button class="xy-btn xy-btn-primary subCheckBtn" lay-submit lay-filter="formBtn">提交</button>
                        <button type="button" class="xy-btn" onclick="resetMethod()">重置</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<script>
    /** 重置 */
    function resetMethod(){
        el.reload();
    }
    layui.use(['layer', 'jquery', 'form', 'notice', 'eleTree'], function () {
        var $ = layui.jquery
            , form = layui.form
            , notice = layui.notice
            , eleTree = layui.eleTree
            , layer = layui.layer;

         el = eleTree({
            el: '#authTree',
            highlightCurrent: true,
            showCheckbox: true,
            isDefaultChangePstatus: true,
            //checkStrictly: true,
            url: basePath + '/roleAuth/getRoleAuthTree',
            where:{
                roleId:roleId
            },
            imgUrl: basePath +"/static/admin/js/layui/3lib/eleTree/images/",
            icon: {
                dropdownOff: "dropdownOff.png",
                dropdownOn: "dropdownOn.png",
                checkFull: "checkFull.png",
                checkHalf: "checkHalf.png",
                checkNone: "checkNone.png",
                fold: "fold.png",
                leaf: "leaf.png",
                loading: ".eleTree-animate-rotate.eletree_icon-loading1",
            },
            })
        //监听提交
        function returnRoleAuthList (){
            var checkArr = el.getChecked(false,true,true);
            var roleAuthList = [];
            for (let i = 0; i < checkArr.length; i++) {
                roleAuthList.push({
                    roleId:roleId,
                    authId:checkArr[i].id
                });
            }
            return roleAuthList;
        }
        form.on('submit(formBtn)', function (data) {
            var loadIndex = null;
            checkSubBtn(1);
            sendAjaxPostToCallbackJson(
                basePath + '/roleAuth/addRoleAuth',
                {
                    roleId:roleId,
                    roleAuthList:returnRoleAuthList()
                },
                function (result){
                    if (result.success){
                        // 提示信息
                        top.layui.notice.msg(result.msg ,{icon: 1,position:"topCenter"});
                        // 获取当前iframe层的索引
                        var index = parent.layer.getFrameIndex(window.name);
                        // 关闭弹窗
                        parent.layer.close(index);
                        // 重载表格
                        parent.layui.table.reload('roleTable',{});
                    }else{
                        checkSubBtn(2);
                        top.layui.notice.msg(result.msg ,{icon: 2,position:"topCenter"});
                    }
                },
                function (data) {
                    checkSubBtn(2);
                    if (utils.string.isEmpty(data) && utils.string.isEmpty(data.msg)){
                        top.layui.notice.msg(data.msg ,{icon: 2,position:"topCenter"});
                    }else{
                        top.layui.notice.msg("服务器内部错误，请联系管理员~", {icon: 2,position:"topCenter"});
                    }
                },
                function (){
                    loadIndex = layer.load(1);
                },
                function (){
                    layer.close(loadIndex);
                }
            )
            return false;
        });
    });
</script>
</body>
</html>
